var wxCharts = require('../../utils/wxcharts.js');
var app = getApp();
var lineChart = null;
var startPos = null;
Page({
    data: {
      answer:[],
      show:false,
      windowWidth:320
    },
    touchHandler: function (e) {
        lineChart.scrollStart(e);
    },
    moveHandler: function (e) {
        lineChart.scroll(e);
    },
    touchEndHandler: function (e) {
        lineChart.scrollEnd(e);
        lineChart.showToolTip(e, {
            format: function (item, category) {
                return category + ' ' + item.name + ':' + item.data 
            }
        });        
    },
    createSimulationData: function () {
        var categories = [];
        var data = [];
        for (var i = 0; i < 7; i++) {
            categories.push('201620162-' + (i + 1));
            data.push(Math.random()*(50-10)+10);
        }
        return {
            categories: categories,
            data: data
        }
    },
    goback(e){
      wx.navigateBack({
        delta:-1
      })
    },
    getAnswer(vid){
      let that=this;
      let param={
   
          vid:vid
      }
      let attr={
          limit:10,
          order:'-updatedAt'
        
      }  
     var windowWidth=this.data.windowWidth
      wx.Als.bmobFunc("vote_date", 'all',param,attr).then(res => {
        if(res.length>0){
            that.setData({
                show:true
            })
          console.log(res)
          var data=[];
          var date=[]
          for(var i=0;i<res.length;i++){
            data.push(res[i].num);
            date.push(res[i].date)
          }
          console.error(data)

          lineChart = new wxCharts({
            canvasId: 'lineCanvas',
            type: 'line',
            categories:date,
            animation: false,
            series: [{
                name: '次数',
                data: data,
                format: function (val, name) {
                    return val.toFixed(2);
                }
            }],
            xAxis: {
                disableGrid: false
            },
            yAxis: {
                title: '次数',
                format: function (val) {
                    return val.toFixed(2);
                },
                min: 0
            },
            width: windowWidth,
            height: 200,
            dataLabel: true,
            dataPointShape: true,
            enableScroll: true,
            extra: {
                lineStyle: 'curve'
            }
        });


        }
           
      
      })


  },
  onShareAppMessage: function() {
    return {
        title: "快来投票吧",
        path: "/pages/index/index"
    };
},
    onLoad: function (e) {
        var windowWidth = 320;
        var tid=e.tid;
        if(!tid){
            wx.showToast({
                title:'不存在',
                icon:'none'
            })
            return
        }

        try {
            var res = wx.getSystemInfoSync();
            windowWidth = res.windowWidth;
            this.setData({
              windowWidth:windowWidth
            })
        } catch (e) {
            console.error('getSystemInfoSync failed!');
        }
        
        var simulationData = this.createSimulationData();
        this.getAnswer(tid)
    
      
        // lineChart = new wxCharts({
        //     canvasId: 'lineCanvas',
        //     type: 'line',
        //     categories: simulationData.categories,
        //     animation: false,
        //     series: [{
        //         name: '次数',
        //         data: simulationData.data,
        //         format: function (val, name) {
        //             return val.toFixed(2);
        //         }
        //     }],
        //     xAxis: {
        //         disableGrid: false
        //     },
        //     yAxis: {
        //         title: '次数',
        //         format: function (val) {
        //             return val.toFixed(2);
        //         },
        //         min: 0
        //     },
        //     width: windowWidth,
        //     height: 200,
        //     dataLabel: true,
        //     dataPointShape: true,
        //     enableScroll: true,
        //     extra: {
        //         lineStyle: 'curve'
        //     }
        // });
    }
});